package com.example.compoundbuttondemo;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private String gender = "保密";
    private String favorite = "Android";
    private String regInfo = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RadioGroup rgGender = findViewById(R.id.rg);
        CheckBox checkAndroid = findViewById(R.id.andr_box);
        CheckBox checkJava = findViewById(R.id.java_box);
        CheckBox checkJsp = findViewById(R.id.jsp_box);
        // 单选按钮成组使用
        rgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                switch (id) {
                    case R.id.male_button:
                        gender = "男";
                        break;
                    case R.id.female_button:
                        gender = "女";
                        break;
                    default:
                        gender = "保密";
                }
            }
        });
        checkAndroid.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b)
                    favorite = favorite + " Android";
                else
                    favorite = favorite.replace("Android", "");
            }
        });

        checkJava.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b)
                    favorite = favorite + " JAVA";
                else
                    favorite = favorite.replace("JAVA", "");
            }
        });
        checkJsp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b)
                    favorite = favorite + " JSP";
                else
                    favorite = favorite.replace("JSP", "");
            }
        });

        Button btnRegister = findViewById(R.id.btn_reg);
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                regInfo = "性别: " + gender + "\n"
                        + "爱好: " + favorite;

                TextView txtShow = findViewById(R.id.show);
                txtShow.setText(regInfo);
            }
        });
    }
}
